package com.one.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.one.bo.MenuBo;
import com.one.dto.SelectMenu;
import com.one.dto.SysMenu;
import com.one.entity.SysMenuEntity;
import com.one.enums.IsDeletedEnum;
import com.one.mapper.SysMenuMapper;
import com.one.service.SysMenuService;
import com.one.utils.DataCheckUtils;
import com.one.utils.DateUtils;
import com.one.utils.TreeUtils;
import com.one.dto.Result;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author zwj
 * @since 2021-09-17
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenuEntity> implements SysMenuService {
    private final SysMenuMapper sysMenuMapper;

    public SysMenuServiceImpl(SysMenuMapper sysMenuMapper) {
        this.sysMenuMapper = sysMenuMapper;
    }

    @Override
    public Result selectMenu(MenuBo menuBo) {
        SelectMenu selectMenu = new SelectMenu();
        selectMenu.setName(menuBo.getName());
        selectMenu.setId(menuBo.getId());
        //查询项目组评分 mybatis
        List<SysMenu> result = sysMenuMapper.selectMenu(selectMenu);
        if (result.size() == 0) return Result.error().message("查询结果为空");
        if(result.size()==1){
            if(selectMenu.getId()!=null && !selectMenu.getId().equals("")){

                return Result.ok().data(result);
            }
            selectMenu.setId(null);
            selectMenu.setName(null);
            selectMenu.setParentIds(result.get(0).getParentIds());
            selectMenu.setParentId(result.get(0).getParentId());
            result = sysMenuMapper.selectMenu(selectMenu);
        }
        return Result.ok().data(TreeUtils.createMenuTree(result,"0"));
    }

    @Override
    public Result deleteMenu(MenuBo menuBo) {
        DataCheckUtils.isNotBlank(menuBo.getId(), "菜单id不能为空!");
        QueryWrapper<SysMenuEntity> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(SysMenuEntity::getId, menuBo.getId()).eq(SysMenuEntity::getIsDeleted, IsDeletedEnum.NO.getKey());;
        SysMenuEntity sysMenuEntity = sysMenuMapper.selectOne(sectionQueryWrapper);
        //查询结果校验
        if (sysMenuEntity == null) {
            return Result.error().message("菜单不存在！");
        }
        SelectMenu selectMenu = new SelectMenu();
        selectMenu.setParentId(sysMenuEntity.getId());
        //查询项目组评分 mybatis
        int row = sysMenuMapper.deleteMenu(selectMenu);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("删除菜单失败");
        }
        return Result.ok();
    }

    @Override
    public Result addMenu(SysMenuEntity sysMenuEntity) {
        //数据校验

        //查询指定工号、身份证号、手机号是否重复
        //插入数据
        sysMenuEntity.setIsDeleted("0");
        sysMenuEntity.setCreateBy("1");
        sysMenuEntity.setCreateTime(DateUtils.getCurrentTime());
        //判断插入结果
        if (SqlHelper.retBool(sysMenuMapper.insert(sysMenuEntity))==false) {
            return Result.error().message("添加菜单失败");
        }
        SysMenuEntity sysMenuEntity1 = new SysMenuEntity();
        sysMenuEntity1.setParentIds(sysMenuEntity.getParentIds()+","+sysMenuEntity.getId());
        UpdateWrapper<SysMenuEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysMenuEntity::getId, sysMenuEntity.getId());
        int row = sysMenuMapper.update(sysMenuEntity1, updateWrapper);
        //判断插入结果
        if (row == 0) {
            return Result.error().message("更新菜单失败");
        }
        return Result.ok();
    }

    @Override
    public Result updateMenu(SysMenuEntity sysMenuEntity) {
        UpdateWrapper<SysMenuEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysMenuEntity::getId, sysMenuEntity.getId());
        int row = sysMenuMapper.update(sysMenuEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("编辑菜单信息失败");
        }
        return Result.ok();
    }
}
